<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">

            <h2 class="title">Pesquisar IPTU</h2><br/>
            <p:growl id="messages" showDetail="false" autoUpdate="true"  /> 

            <h:form id="userForm" style="margin: 0 30px; " enctype="multipart/form-data">

                <h4 class="title">Fator Localização</h4> 

                <p:panel id="fatorPanel" style="background: none; border: none;" 
                         styleClass="container_24 clearfix">


                    <p:outputLabel value="Inscrição Imóvel" styleClass="grid_4 inscricaoLabelImovel" for="maskinscricao" />
                    <p:inputMask mask="?**.**.***.****.**" id="maskinscricao" 
                                 styleClass="grid_6"  value="#{managerPesquisarIptu.visaoInscricao}"/>
                    <p:watermark value="DD.SS.QUADR.LOTE.UN" for="maskinscricao" />

                    <div class="clear"/>
                </p:panel>

                <h4 class="title">Proprietário</h4>

                <p:panel id="ownerPanel" style="background: none; border: none;" 
                         styleClass="container_24 clearfix">
                    <p:outputLabel value="Proprietário" styleClass="grid_3 proprietarioLabelPesquisar"/>
                    <p:selectOneRadio id="typePerson" 
                                      value="#{managerPesquisarIptu.proprietario.tipoDocumento}" >  
                        <f:selectItems value="#{managerPesquisarIptu.tipoDocumento()}" />
                        <p:ajax event="change" update=":userForm:ownerPanel" />
                    </p:selectOneRadio>

                    <div class="clear"/>

                    <p:outputLabel id="typeDocumentLabel" 
                                   value="#{managerPesquisarIptu.proprietario.tipoDocumento}: " 
                                   styleClass="grid_3"/>

                    <f:validateBean disabled="true">
                        <p:inputMask id="typeDocumentInput" 
                                     value="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                              managerPesquisarIptu.proprietario.cpf : managerPesquisarIptu.proprietario.cnpj}"
                                     mask="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                             '999.999.999-99':'99.999.999/9999-99'}" 
                                     styleClass="grid_5" > 

                        </p:inputMask>
                    </f:validateBean>
                    <div class="clear"/>
                    <p:outputLabel value="Nome: " styleClass="grid_3" for="nomeInputText" 
                                   rendered="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CPF'}"/>
                    <p:inputText id="nomeInputText" styleClass="grid_19" 
                                 value="#{managerPesquisarIptu.proprietario.nome}" 
                                 rendered="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                    <p:outputLabel value="Fantasia: " styleClass="grid_3" for="nameFantasyInputText" 
                                   rendered="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                    <p:inputText id="nameFantasyInputText" styleClass="grid_19"
                                 value="#{managerPesquisarIptu.proprietario.nomeFantasia}" 
                                 rendered="#{managerPesquisarIptu.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />

                </p:panel>

                <h4 class="title">Endereço</h4>

                <p:panel id="addressPanel" style="background: none; border: none;" 
                         styleClass="container_24 clearfix ">
                    <p:outputLabel value="CEP" styleClass="grid_3" for="cepInputMask" />
                    <p:inputMask id="cepInputMask" styleClass="grid_4" mask="99.999-999"
                                 value="#{managerPesquisarIptu.endereco.cep}" />
                    <div class="clear-both" />

                    <p:outputLabel value="Número" styleClass="grid_3" for="numInputText" />
                    <p:inputText id="numInputText" styleClass="grid_2"
                                 value="#{managerPesquisarIptu.endereco.numero}" />

                    <p:outputLabel value="Logradouro" styleClass="grid_4 alpha" for="patioInputText" />
                    <p:inputText id="patioInputText" styleClass="grid_13"
                                 value="#{managerPesquisarIptu.endereco.logradouro}" />

                    <div class="clear-both" />
                    <p:outputLabel value="Bairro" styleClass="grid_3" for="neighborhoodInputText" />
                    <p:inputText id="neighborhoodInputText" styleClass="grid_19"
                                 value="#{managerPesquisarIptu.endereco.bairro}" />

                </p:panel>

                <br/>
                <div class="buttonAction">
                    <p:commandButton id="pesquisarCommandButton" value="Pesquisar" icon="ui-icon-search"
                                     update="@form"
                                     actionListener="#{managerPesquisarIptu.pesquisarIptu()}"
                                     styleClass="ui-priority-primary" />
                    <p:commandButton id="bt_clear" value="Limpar" icon="ui-icon-clean"
                                     actionListener="#{managerPesquisarIptu.limpar()}"
                                     update="@form" process="@this" />
                </div>
                <br/>

                <p:blockUI id="BLOCK" block="resultPanel" trigger="pesquisarCommandButton, tableImovel" > 
                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                </p:blockUI> 

                <div class="container_24 clearfix">
                    <div class="grid_10" style="text-align: right; float: right">
                        <div class="grid_12"><h5>Regular</h5></div>
                        <div class="grid_3" style="background-color: green; border-radius: 100%; width: 23px; color: green;">*</div>

                        <div class="grid_7"><h5>Pendente</h5></div>
                        <div class="grid_3" style="background-color: red; border-radius: 100%; width: 23px; color: red;">*</div>
                    </div>
                </div>

                <p:panel id="resultPanel" styleClass="container_24 clearfix ">

                    <p:dataTable id="tableImovel" var="item" value="#{managerPesquisarIptu.dataModel}"
                                 styleClass="grid_24" rowIndexVar="rowid" 
                                 style="white-space: pre-wrap;"
                                 binding="#{managerPesquisarIptu.searchResults}"
                                 rows="5" paginator="true" lazy="true"
                                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                                 currentPageReportTemplate="{currentPage} de {totalPages}"
                                 paginatorPosition="bottom" emptyMessage="Nenhum registro">
                        <p:column headerText="Resultados" >
                            <p:dataTable value="#{managerPesquisarImovel.iptus(item)}" var="i" emptyMessage="Nenhum registro"> 
                                <f:facet name="header" >
                                    <div class="clearfix" style="text-align:left;">
                                        Proprietário: #{managerPesquisarIptu.contribuinte(item)}<br/>
                                        Logradouro: #{item.enderecoImovel.nomeclog eq null or item.enderecoImovel.nomeclog eq '' ? item.enderecoImovel.logradouro : item.enderecoImovel.nomeclog}, Nº 
                                        #{item.enderecoImovel.numero} , bairro #{item.enderecoImovel.bairro}
                                    </div>
                                    <div  class="clearfix"  style="float: right; margin-top: -35px;">
                                        <p:button title="Visualizar" value="Visualizar"
                                                  href="visualizarimovel.xhtml?vImovel=#{item.id}"
                                                  icon="ui-icon-zoomin"/>
                                    </div>
                                </f:facet>
                                <p:column style="width:16px">
                                    <p:rowToggler />
                                </p:column>
                                <p:column headerText="#" width="20">
                                    <div style="border-radius: 100%; width: 23px; height: 23px; background-color: #{managerPesquisarImovel.existeParcelaPendencia(i) ? 'red':'green'}">

                                    </div>
                                </p:column>
                                <p:column headerText="Exercício">
                                    #{managerPesquisarImovel.anoIptu(i)}
                                </p:column>
                                <p:column headerText="Situação">
                                    #{managerPesquisarImovel.existeParcelaPendencia(i) ? 'Pendente':'Regular'}
                                </p:column>
                                <p:rowExpansion>
                                    <p:dataTable var="p"  value="#{managerPesquisarImovel.parcelas(i)}">
                                        <f:facet name="header">
                                            Detalhes - #{managerPesquisarImovel.anoIptu(i)}
                                        </f:facet>
                                        <p:column headerText="Ação"> 
                                            <p:commandButton title="Imprimir"
                                                             ajax="false" value="" update="@form"
                                                             style="width: 35px !important; height: 30px !important; #{p.pago eq true or p.baixaManual eq true ? 'display:none': ''}" 
                                                             actionListener="#{managerPesquisarIptu.geraBoleto()}" rendered="#{managerPesquisarIptu.possivelBaixa(i, p)}"
                                                             icon="ui-icon-print" >
                                                <f:param name="iptuParam" value="#{i.id}"/>
                                                <f:param name="parcelaParam" value="#{p.id}"/>
                                            </p:commandButton>
                                            <p:commandButton title="Dar Baixa" value=""
                                                             style="width: 35px !important; height: 30px !important; #{p.pago eq true or p.baixaManual eq true ? 'display:none': ''}" 
                                                             onclick="dlgBaixaIPTU.show()" actionListener="#{managerPesquisarIptu.setDadosBaixa}" rendered="#{managerPesquisarIptu.possivelBaixa(i, p)}"
                                                             icon="ui-icon-check" >
                                                <f:param name="iptuParam" value="#{i.id}"/>
                                                <f:param name="parcelaParam" value="#{p.id}"/>
                                            </p:commandButton>
                                        </p:column>
                                        <p:column headerText="Status"> 
                                            <p:outputLabel style="#{p.pago eq true || p.baixaManual eq true ? 'color: green': (p.indice eq 0 and (i.pago or i.baixaManual)) ? 'color: black' : 'color: red'}" value=" #{p.pago eq true || p.baixaManual eq true ? 'Pago': (p.indice eq 0 and (i.pago or i.baixaManual)) ? ' - ' : 'Aberto'}" />
                                        </p:column>
                                        <p:column headerText="Parcela">
                                            <p:outputLabel value="#{p.indice eq 0 ? 'Única' : '' || 
                                                                    p.indice eq 1 ? '1˚ Parcela' : '' ||
                                                                    p.indice eq 2 ? '2˚ Parcela' : '' ||
                                                                    p.indice eq 3 ? '3˚ Parcela' : '' ||
                                                                    p.indice eq 4 ? '4˚ Parcela' : ''  ||
                                                                    p.indice eq 5 ? '5˚ Parcela' : '' ||
                                                                    p.indice eq 6 ? '6˚ Parcela' : '' ||
                                                                    p.indice eq 7 ? '7˚ Parcela' : '' ||
                                                                    p.indice eq 8 ? '8˚ Parcela' : '' ||
                                                                    p.indice eq 9 ? '9˚ Parcela' : '' ||
                                                                    p.indice eq 10 ? '10˚ Parcela' : '' ||
                                                                    p.indice eq 11 ? '11˚ Parcela' : '' ||
                                                                    p.indice eq 12 ? '12˚ Parcela' : '' }"/>
                                        </p:column>
                                        <p:column headerText="Valor"> 
                                            <p:outputLabel value="R$ #{p.valor}" >
                                                <f:convertNumber pattern="#,###,##0.00" locale="pt,BR"/>
                                            </p:outputLabel>
                                        </p:column>
                                        <p:column headerText="Data Vencimento">
                                            <h:outputText value="#{p.dataVencimento}">
                                                <f:convertDateTime  pattern="dd/MM/YYYY"/>
                                            </h:outputText>
                                        </p:column>

                                    </p:dataTable>
                                </p:rowExpansion>
                            </p:dataTable>
                        </p:column>
                    </p:dataTable> 
                </p:panel>
                <p:dialog id="dialogBaixaIPTU" widgetVar="dlgBaixaIPTU" header="Baixa em Parcela" modal="true"
                          height="300" width="700" closable="true" resizable="false" >
                    <p:panel id="motivo" styleClass="container_24 clearfix ">

                        <p:outputLabel value="Motivo da baixa manual"/>
                        <p:inputTextarea value="#{managerPesquisarIptu.motivoBaixa}"/>

                    </p:panel>
                    <div class="buttonAction">

                        <p:commandButton value="Dar Baixa" actionListener="#{managerPesquisarIptu.baixaManualIPTU()}"
                                         update="@form" style="width: 153px;margin-top: 10px"
                                         icon="ui-icon-check" immediate="true" onkeyup="dlg.hide()"/>
                    </div>
                </p:dialog>

            </h:form>
        </ui:define>
    </ui:composition>
</html>
